package com.example.wujinapi.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.wujinapi.dao.ProductsDao;
import com.example.wujinapi.entity.Products;
import com.example.wujinapi.service.ProductsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * (Products)表服务实现类
 *
 * @author makejava
 * @since 2023-07-10 22:08:37
 */
@Service("productsService")
public class ProductsServiceImpl extends ServiceImpl<ProductsDao, Products> implements ProductsService {

    @Autowired
    private ProductsDao productsDao;

    @Override
    public void addProduct(Products products) {
        Products product=productsDao.selectOne(new QueryWrapper<Products>().eq("productname", products.getProductname()));
        if(product==null){ //为空才插入新用户
            productsDao.insert(products);
        }
    }

    @Override
    public void updateProduct(Products products) {
        Products pdata=productsDao.selectOne(new QueryWrapper<Products>().eq("id", products.getId()));
        if(pdata!=null){
            products.setId(pdata.getId());
            productsDao.updateById(products);
        }
    }

    @Override
    public void deleteProduct(int productId) {
        productsDao.delete(new QueryWrapper<Products>().eq("id",productId));
    }

    @Override
    public int getAllProductsTotals() {
        return productsDao.getAllProductsTotals(); //所有产品总数
    }

    @Override
    public Products getProductById(int id) {
        Products pdata=productsDao.selectOne(new QueryWrapper<Products>().eq("id", id).eq("isshow",1)); //1显示 0不显示
        return pdata!=null?pdata:null;
    }

    @Override
    public List<Products> searchProducts(String keyWord) {
        List<Products> pListData=productsDao.selectList(new QueryWrapper<Products>().like("productname", keyWord).eq("isshow",1)); //1显示 0不显示
        return pListData!=null?pListData:null;
    }

    @Override
    public List<Map<String, Object>> getProducts(int pageNum, int pageSize) {
        Page<Products> page = new Page<Products> (pageNum,pageSize); //实现定义的接口
        List<Map<String,Object>> Datas=productsDao.selectPageVo(page);
        return Datas;
    }
}

